﻿using System;
using System.IO;
using System.Web.UI;
using PAS.Helper;

namespace PAS
{
    public partial class DataImport : Page
    {
        private AbstractImportor Importor
        {
            get { return (AbstractImportor) Session["Importor"]; }
            set { Session["Importor"] = value; }
        }

        private object BindErrorMessages()
        {
            return Importor.ErrMessage;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            pgError.DataSourceDelegate = BindErrorMessages;
            btnImport.Attributes.Add("onclick",
                                     "setTimeout('document.all(\"" + btnImport.ClientID + "\").disabled=true;',10);");
            btnCheckData.Attributes.Add("onclick",
                                        "setTimeout('document.all(\"" + btnCheckData.ClientID +
                                        "\").disabled=true;',10);");
            if (!IsPostBack)
            {
                Importor = null;
                tblBaseInfo.Visible = true;
            }
        }

        protected void btnCheckData_Click(object sender, EventArgs e)
        {
            Importor = null;
            btnImport.Enabled = false;
            DateTime S_datetime = DateTime.Now;
            lblMessage.Text = "";
            if (fileSource.Value == "")
            {
                lblCheckSource.Text = "请选择导入文件";
                return;
            }

            string strFileName = fileSource.PostedFile.FileName;
            int index = strFileName.LastIndexOf("\\");
            strFileName = strFileName.Substring(index + 1, strFileName.Length - index - 1);

            if (strFileName.Substring(strFileName.Length - 4).ToLower() != ".xls")
            {
                lblMessage.Text = "请使用正确的模板文件";
                pgError.Visible = false;
                return;
            }

            string path = Path.GetTempPath() + DateTime.Now.Ticks.ToString().Substring(5) + strFileName;
            fileSource.PostedFile.SaveAs(path);

            //try
            //{
                if (rblSheetName.SelectedValue == "0")
                    Importor = new PeopleImportor(path, rblSheetName.SelectedItem.Text);
                else if (rblSheetName.SelectedValue == "1")
                    Importor = new OptionImportor(path, rblSheetName.SelectedItem.Text);
                if (Importor == null) return;
                Importor.CheckData();
                int rightDataCount = Importor.RightDataLocations.Count;
                int errorDataCount = Importor.ErrDateLocations.Count;
                if (Importor.ErrMessage == null || Importor.ErrMessage.Rows.Count == 0)
                {
                    pgError.Visible = false;
                }
                else
                {
                    pgError.Visible = true;
                    pgError.SortExpression = "行号";
                    pgError.BindGrid();
                }

                DateTime F_Datetime = DateTime.Now;
                TimeSpan showtime = F_Datetime - S_datetime;
                lblMessage.Text = "正确数据： " + rightDataCount + " 条，错误数据： " + errorDataCount + " 条，校验所耗时间：" +
                                  showtime.Hours + " 时 " +
                                  showtime.Minutes + " 分 " + showtime.Seconds + " 秒";
                btnImport.Enabled = rightDataCount > 0;
            //}
            //catch (ArgumentException ex)
            //{
            //    Alert.MessageBox(ex.Message);
            //}
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            DateTime S_datetime = DateTime.Now;
            lblMessage.Text = "";
            try
            {
                Importor.SaveRightData();

                pgError.Visible = false;

                DateTime F_Datetime = DateTime.Now;
                TimeSpan showtime = F_Datetime - S_datetime;
                lblMessage.Text = "导入数据： " + Importor.RightDataLocations.Count + " 条，导入所耗时间：" + showtime.Hours + " 时 " +
                                  showtime.Minutes + " 分 " + showtime.Seconds +
                                  " 秒";
            }
            catch (ArgumentException ex)
            {
                Alert.MessageBox(ex.Message);
            }
            finally
            {
                Importor = null;
                btnImport.Enabled = false;
            }
        }

        protected void btnDownFiles_Click(object sender, EventArgs e)
        {
        }
    }
}